package org.example.domain.activity.service.partake;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.activity.model.arrtrgate.CreatePartakeOrderAggregate;
import org.example.domain.activity.model.entity.ActivityEntity;
import org.example.domain.activity.model.entity.PartakeRaffleActivityEntity;
import org.example.domain.activity.model.entity.UserRaffleOrderEntity;
import org.example.domain.activity.model.valobj.ActivityStateVO;
import org.example.domain.activity.repository.IActivityRepository;
import org.example.domain.activity.service.IRaffleActivityPartakeService;
import org.example.types.enums.ResponseCode;
import org.example.types.exception.AppException;

import java.util.Date;

@Slf4j
public abstract class AbstractRaffleActivityPartake implements IRaffleActivityPartakeService {


    protected final IActivityRepository activityRepository;

    public AbstractRaffleActivityPartake(IActivityRepository activityRepository) {
        this.activityRepository = activityRepository;
    }

    @Override
    public UserRaffleOrderEntity createOrder(PartakeRaffleActivityEntity partakeRaffleActivityEntity) {
        String userId = partakeRaffleActivityEntity.getUserId();
        Long activityId = partakeRaffleActivityEntity.getActivityId();
        Date currentDate = new Date();

        //1.活动查询
        ActivityEntity activityEntity = activityRepository.queryActivityEntityByactivityId(activityId);

        //校验活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        //活动时间的校验
        if (currentDate.before(activityEntity.getBeginDateTime()) || currentDate.after(activityEntity.getEndDateTime())){
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        //2.查询是否存在未使用的订单
        UserRaffleOrderEntity userRaffleOrderEntity =  activityRepository.queryNoUsedRaffleOrder(partakeRaffleActivityEntity);

        if (null != userRaffleOrderEntity){
            log.info("创建参与活动订单[已存在未消费] userId:{} activityId:{} userRaffleOrderEntity:{}" ,userId,activityId,userRaffleOrderEntity);
            return userRaffleOrderEntity;
        }
        //过滤账户,判断账户额度是否足够
        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAccount( userId, activityId,currentDate);

        //构建订单
        UserRaffleOrderEntity userRaffleOrder =  this.buildUserRaffleOrder(userId,activityId,currentDate);
        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrder);

        activityRepository.savecreatePartakeOrderAggregate(createPartakeOrderAggregate);
        return userRaffleOrder;

    }

    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentDate);

    protected abstract CreatePartakeOrderAggregate doFilterAccount(String userId, Long activityId, Date currentDate);

}
